<?php 

class HangHoaController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
    
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteHanghoa($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $HH_HANG_HOA=$_POST['HH_HANG_HOA'];
            $DVT_ID=$_POST['DVT_ID'];
            $HH_DON_GIA_NHAP=$_POST['HH_DON_GIA_NHAP'];
            $HH_DON_GIA_BAN=$_POST['HH_DON_GIA_BAN'];
            $HH_DON_GIA_SI=$_POST['HH_DON_GIA_SI'];
            $HH_DON_GIA_LE=$_POST['HH_DON_GIA_LE'];
            $result = $this->addHangHoa($HH_HANG_HOA, $DVT_ID, $HH_DON_GIA_NHAP, $HH_DON_GIA_BAN, $HH_DON_GIA_SI, $HH_DON_GIA_LE);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_hanghoa");
                }
            }
             exit();
             
             
        } else if ($action == 'edit' ) {

            $HH_ID=$_POST['H_ID'];
            $HH_HANG_HOA=$_POST['HH_HANG_HOA'];
            $DVT_ID=$_POST['DVT_ID'];
            $HH_DON_GIA_NHAP=$_POST['HH_DON_GIA_NHAP'];
            $HH_DON_GIA_BAN=$_POST['HH_DON_GIA_BAN'];
            $HH_DON_GIA_SI=$_POST['HH_DON_GIA_SI'];
            $HH_DON_GIA_LE=$_POST['HH_DON_GIA_LE'];
            $result = $this->updateHangHoa($HH_ID,$HH_HANG_HOA, $DVT_ID, $HH_DON_GIA_NHAP, $HH_DON_GIA_BAN, $HH_DON_GIA_SI, $HH_DON_GIA_LE);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_hanghoa");
                }
            }
             exit();
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_hanghoa' . DS . 'views' . DS . 'hanghoa_add.php');
            
            $donvitinhs=$this->getAllDonvitinh();
            
              if ($donvitinhs != null) {
                 
                foreach ($donvitinhs as $donvitinh) {

                     
                     $xtemplate->assign('DVT_ID', $donvitinh->getDVT_ID());
                    $xtemplate->assign('DVT_DVT', $donvitinh->getDVT_DVT());
                    $xtemplate->parse('main.donvitinh');
                    
                }
            }
            
            
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idhanghoa'];
            $viewFile = $dir . '/views/hanghoa_edit.php';
            $xtemplate = new XTemplate($viewFile);
            $hanghoa = $this->getHangHoaById($id);
//            Utility::messageBox($id);

            if ($hanghoa != null) {               

                    $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP', $hanghoa->getDON_GIA_NHAP());
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    $donvitinhs=$this->getAllDonvitinh();
                    $option_donvitinh='';
                    if ($donvitinhs != null) {

                      foreach ($donvitinhs as $donvitinh) {
                          if($hanghoa->getDON_VI_TINH_ID()==$donvitinh->getDVT_ID()){
                          $option_donvitinh .="<option selected=selected value='".$donvitinh->getDVT_ID()."'>".$donvitinh->getDVT_DVT()."</option>";
                          }else{
                             $option_donvitinh .="<option  value='".$donvitinh->getDVT_ID()."'>".$donvitinh->getDVT_DVT()."</option>";
                            
                          }

                      }
                  }                  
                    
                    $xtemplate->assign('DVT', $option_donvitinh);
                    $xtemplate->parse('main.hanghoa');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/hanghoa.php';
            $xtemplate = new XTemplate($viewFile);
            $hanghoas = $this->getAllHangHoa();


            if ($hanghoas != null) {
                  $indexCheckBox = 1;
                foreach ($hanghoas as $hanghoa) {

                     $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);
                     $xtemplate->assign('H_ID', $hanghoa->getID());
                    $xtemplate->assign('H_TEN', $hanghoa->getTEN_HANG_HOA());
                    $xtemplate->assign('H_GIA_NHAP',Utility::formatToCurrencyString($hanghoa->getDON_GIA_NHAP()) );
                    $xtemplate->assign('H_GIA_BAN', $hanghoa->getDON_GIA_BAN());
                    $xtemplate->assign('H_GIA_BAN_SI', $hanghoa->getDON_GIA_SI());
                    $xtemplate->assign('H_GIA_BAN_LE', $hanghoa->getDON_GIA_LE());
                    
                    
                    
                    
                    $xtemplate->parse('main.hanghoa');
                    $indexCheckBox++;
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        
        
        
        
        
        
        
        
    }

    private function getAllHangHoa() {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();

            $result = $hanghoa->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getHangHoaById($id) {
        $db = new Database();
        try {
            $hanghoa = new Hanghoa();
            $hanghoa->setID($id);
            $result = $hanghoa->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
        private function getAllDonvitinh() {
        $db = new Database();
        try {
            $donvitinh = new Donvitinh();

            $result = $donvitinh->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function addHangHoa($HH_HANG_HOA, $DVT_ID, $HH_DON_GIA_NHAP, $HH_DON_GIA_BAN, $HH_DON_GIA_SI, $HH_DON_GIA_LE){
        
         $db = new Database();
        try {            
           
                $hanghoa = new Hanghoa();
                $hanghoa->setTEN_HANG_HOA($HH_HANG_HOA);
                 $hanghoa->setDON_VI_TINH_ID($DVT_ID);
                  $hanghoa->setDON_GIA_NHAP($HH_DON_GIA_NHAP);
                   $hanghoa->setDON_GIA_BAN($HH_DON_GIA_BAN);
                    $hanghoa->setDON_GIA_SI($HH_DON_GIA_SI);
                     $hanghoa->setDON_GIA_LE($HH_DON_GIA_LE);
                $hanghoa->insert($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
        private function updateHangHoa($HH_ID,$HH_HANG_HOA, $DVT_ID, $HH_DON_GIA_NHAP, $HH_DON_GIA_BAN, $HH_DON_GIA_SI, $HH_DON_GIA_LE){
        
         $db = new Database();
        try {            
           
                $hanghoa = new Hanghoa();
                 $hanghoa->setID($HH_ID);
                $hanghoa->setTEN_HANG_HOA($HH_HANG_HOA);
                 $hanghoa->setDON_VI_TINH_ID($DVT_ID);
                  $hanghoa->setDON_GIA_NHAP($HH_DON_GIA_NHAP);
                   $hanghoa->setDON_GIA_BAN($HH_DON_GIA_BAN);
                    $hanghoa->setDON_GIA_SI($HH_DON_GIA_SI);
                     $hanghoa->setDON_GIA_LE($HH_DON_GIA_LE);
                $hanghoa->update($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
        private function deleteHanghoa($values) {
        $db = new Database();
        try {            
           
                $hanghoa = new Hanghoa();
                $hanghoa->setID($values);
                $hanghoa->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
